<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Publico extends MY_Controller {

  public function _remap($method){
    // $this->carabiner->css(array(
    //     array('base.css', 'screen')
    // ));
    // $this->carabiner->js(array(
    //     array('libs/jquery.min.js'),
    //     array('libs/jquery-ui.js'),
    //     array('alertas.js'),
    //     array('libs/jquery.notify.min.js'),
    // ));

    $this->{$method}();
  }

  public function index()
  {
    redirect(base_url());
  }

  /**
   *    Visualiza el contenido del articulo
   */
  public function a()
  {
    $this->load->model('articulos_model');
    $data = array('tipo' => 'a');
    $data['info'] = $this->articulos_model->get_articulo_info($_GET['id']);

    if (count($data['info']['info']) > 0)
    {
      if($this->session->userdata('id_usuario'))
      {
        if ($this->session->userdata['tipo'] === 'tic')
          $this->tic($data);
        else
          $this->free($data);
      }
      else
        $this->no_login($data);
    }
    else
        redirect(base_url());
  }

  /**
   *    Visualiza la o las infografias
   */
  public function i()
  {

    $info = array('tipo' => 'i');
    if (isset($_GET['ida']))
    {
      $result = $this->db->query("SELECT id_imagen, id_articulo, url, url_thumb, titulo
                                 FROM articulos_imagenes
                                 WHERE id_articulo = " . $_GET['ida']);

      if ($result->num_rows() > 0)
      {
        foreach ($result->result() as $v)
        {
          $info['data'][] = array('titulo' => $v->titulo, 'url' => $v->url, 'thumb' => $v->url_thumb);
        }
      }
      else
        redirect(base_url());
    }
    else
    {
      $ids = explode(',', $_GET['idi']);
      foreach ($ids as $id)
      {
        $query = $this->db->query("SELECT id_imagen, id_articulo, url, url_thumb, titulo
                                   FROM articulos_imagenes
                                   WHERE id_imagen = " . $id);


        if ($query->num_rows() > 0)
        {
          $data = $query->result();
          $info['data'][] = array('titulo' => $data[0]->titulo, 'url' => $data[0]->url, 'thumb' => $data[0]->url_thumb);
        }
      }

      if (!isset($info['data']))
        redirect(base_url());
    }

    if($this->session->userdata('id_usuario'))
    {
      if ($this->session->userdata['tipo'] === 'tic')
        $this->tic($info);
      else
        $this->free($info);
    }
    else
      $this->no_login($info);
  }

  public function no_login($data)
  {
    $this->carabiner->css(array(
      array('base.css', 'screen'),
      array('libs/jquery.tinyscroll.css', 'screen'),
      array('libs/ui.notify.css', 'screen'),
      array('libs/jquery.slider.css', 'screen'),
      array('home.css', 'screen'),
      array('articulos.css', 'screen'),
    ));
    $this->carabiner->js(array(
      array('libs/jquery.min.js'),
        array('libs/jquery-ui.js'),
        array('alertas.js'),
        array('libs/jquery.notify.min.js'),
        array('libs/jquery.html5placeholder.js'),
        array('libs/jquery.tinyscrollbar.min.js'),
        array('libs/jquery.slides.min.js'),
        array('http://connect.facebook.net/en_US/all.js'),
        array('home/home.js')
    ));

    $params['seo'] = array('titulo' => 'Tienda Capp breviarios, articulos, infografias de política pública',
      'description' => 'Análisis mensual de temas de interés sobre Colima. Dos páginas de conocimiento preciso para tomadores de decisiones. Relevantes estadísticas, datos e información acerca de Colima.');

    if (isset($_POST['entrar']))
      $fun_res = $this->registro();
    if (isset($_POST['tic']) || isset($_POST['free']))
      $fun_res = $this->login();

    if (isset($fun_res['frm_errors'])){
      $params['frm_errors'] = $fun_res['frm_errors'];
    }

    $params['organiza'] = $this->db
      ->select('id_organizacion, nombre, estudiante')
      ->from('organizaciones')
      ->order_by('nombre', 'ASC')
      ->get()->result();

    if (isset($_GET['msg'])) {
      $params['frm_errors'] = $this->showMsgs($_GET['msg'], 'Error');
    }

    $this->load->library('my_facebook');
    $params['fb_app_id'] = $this->my_facebook->APP_ID;

    // var_dump($data);
    $params['info'] = $data;

    $this->load->view('skin/header', $params);
    $this->load->view('skin/publico/free-nolog', $params);
    $this->load->view('skin/footer');
  }

  public function free($data)
  {
    $this->carabiner->css(array(
      array('bootstrap/bootstrap-simplex.css', 'screen'),
      array('base.css', 'screen'),
      array('libs/jquery.tinyscroll.css', 'screen'),
      array('libs/jquery.smoothDivScroll.css', 'screen'),
      array('libs/jquery.slider.css', 'screen'),
      array('free.css', 'screen'),
      array('barra_rfree.css', 'screen'),

      array('libs/jquery.superbox.css', 'screen'),
       array('articulos.css', 'screen'),
    ));
    $this->carabiner->js(array(
        array('libs/jquery.min.js'),
        array('bootstrap/bootstrap-dropdown.js'),
        array('libs/jquery.html5placeholder.js'),
        array('libs/jquery.tinyscrollbar.min.js'),
        array('libs/jquery-ui.js'),
        array('libs/jquery.mousewheel.min.js'),
        array('libs/jquery.smoothdivscroll-1.2-min.js'),
        array('libs/jquery.slides.min.js'),
        array('free/free.js'),
        array('http://connect.facebook.net/en_US/all.js'),
        array('free/barra_rfree.js'),
        array('home/checker.js'),
        array('libs/jquery.superbox.js')
    ));

    $params['seo'] = array('titulo' => $data['info']['info'][0]->titulo.' - Tienda Capp');

    // $this->load->model('skin_model');

    // $params['bpp'] = $this->db->query("SELECT id_articulo, numero, titulo, imagen1, fecha_creacion
    //   FROM articulos WHERE status = 1 AND id_tipo = 1 ORDER BY numero DESC")->result();

    // $params['cp'] = $this->db->query("SELECT id_articulo, numero, titulo, imagen1, fecha_creacion
    //   FROM articulos WHERE status = 1 AND id_tipo = 2 ORDER BY numero DESC")->result();

    // $params['bd'] = $this->db->query("SELECT id_articulo, numero, titulo, imagen1, fecha_creacion
    //   FROM articulos WHERE status = 1 AND id_tipo = 3 ORDER BY numero DESC")->result();

    // $params['ventas'] = $this->skin_model->getAsesoriaVenta();

    // if ($this->session->userdata('tipo') == 'tic') {
    //   $params['barra_r'] = $this->load->view('skin/free/barra_right_free', $params, true);
    // }else{
    //   $params['barra_r'] = $this->load->view('skin/free/barra_right_free', $params, true);
    // }

    // var_dump($data);
    $params['info'] = $data;

    $this->load->library('my_facebook');
    $params['fb_app_id'] = $this->my_facebook->APP_ID;

    $this->load->view('skin/header', $params);
    $this->load->view('skin/publico/free-nolog', $params);
    $this->load->view('skin/footer');
  }

  public function tic($data)
  {
    $this->carabiner->css(array(
        array('bootstrap/bootstrap-simplex.css', 'screen'),
        array('bootstrap/bootstrap-responsive.css', 'screen'),
        array('bootstrap/charisma-app.css', 'screen'),
        array('bootstrap/jquery-ui-1.8.21.custom.css', 'screen'),
        array('bootstrap/fullcalendar.css', 'screen'),
        array('bootstrap/fullcalendar.print.css', 'print'),
        array('bootstrap/chosen.css', 'screen'),
        array('bootstrap/uniform.default.css', 'screen'),
        array('bootstrap/colorbox.css', 'screen'),
        array('bootstrap/jquery.cleditor.css', 'screen'),
        array('bootstrap/jquery.noty.css', 'screen'),
        array('bootstrap/noty_theme_default.css', 'screen'),
        array('bootstrap/elfinder.min.css', 'screen'),
        array('bootstrap/elfinder.theme.css', 'screen'),
        array('bootstrap/jquery.iphone.toggle.css', 'screen'),
        array('bootstrap/opa-icons.css', 'screen'),
        array('bootstrap/uploadify.css', 'screen'),

        array('base-tic.css', 'screen'),
        array('libs/jquery.slider.css', 'screen'),
        array('libs/jquery.allinone_carousel.css', 'screen'),

        array('libs/jquery.scrollbarpaper.css', 'screen'),
        // array('libs/jquery.smoothDivScroll.css', 'screen'),
    ));
    $this->carabiner->js(array(
        array('bootstrap/jquery-1.7.2.min.js'),
        array('bootstrap/jquery-ui-1.8.21.custom.min.js'),
        array('bootstrap/bootstrap-transition.js'),
        array('bootstrap/bootstrap-alert.js'),
        array('bootstrap/bootstrap-modal.js'),
        array('bootstrap/bootstrap-dropdown.js'),
        array('bootstrap/bootstrap-scrollspy.js'),
        array('bootstrap/bootstrap-tab.js'),
        array('bootstrap/bootstrap-tooltip.js'),
        array('bootstrap/bootstrap-popover.js'),
        array('bootstrap/bootstrap-button.js'),
        array('bootstrap/bootstrap-collapse.js'),
        array('bootstrap/bootstrap-carousel.js'),
        array('bootstrap/bootstrap-typeahead.js'),
        array('bootstrap/bootstrap-tour.js'),
        array('bootstrap/jquery.cookie.js'),
        array('bootstrap/fullcalendar.min.js'),
        array('bootstrap/jquery.dataTables.min.js'),
        /** chart libraries **/
        array('bootstrap/excanvas.js'),
        array('bootstrap/jquery.flot.min.js'),
        array('bootstrap/jquery.flot.pie.min.js'),
        array('bootstrap/jquery.flot.stack.js'),
        array('bootstrap/jquery.flot.resize.min.js'),

        array('bootstrap/jquery.chosen.min.js'),
        array('bootstrap/jquery.uniform.min.js'),
        array('bootstrap/jquery.colorbox.min.js'),
        array('bootstrap/jquery.cleditor.min.js'),
        array('bootstrap/jquery.noty.js'),
        array('bootstrap/jquery.elfinder.min.js'),
        array('bootstrap/jquery.raty.min.js'),
        array('bootstrap/jquery.iphone.toggle.js'),
        array('bootstrap/jquery.autogrow-textarea.js'),
        array('bootstrap/jquery.uploadify-3.1.min.js'),
        array('bootstrap/jquery.history.js'),
        array('bootstrap/charisma.js'),
        array('libs/jquery.slides.min.js'),
        array('libs/jquery.smoothdivscroll-1.2-min.js'),
        array('libs/jquery.allinone_carousel.js'),

        array('libs/jquery.scrollbarpaper.min.js'),

        array('free/free.js'),
        // array('tic/favoritos.js'),
        array('tic/carrito.js'),
        array('tic/footer.js'),
        array('http://connect.facebook.net/en_US/all.js'),
        array('tic/publicaciones.js'),
        array('home/publico.js'),
    ));

    $params['info_empleado'] = $this->info_empleado['info']; //info empleado
    $params['seo'] = array(
      'titulo' => $data['info']['info'][0]->titulo.' - Tienda Capp'
    );


    //url redirect de las paginas
    $params['url_redirect'] = base_url('tic/');


    /**** Carro de compras ****/
    //msg si se realiza alguna operacion
    $params['mod_compra'] = null;
    //obtiene acceso tic ilim
    $this->load->model('skin_model');
    $acc_ilim = $this->db->query("SELECT ticDiasFaltantesIlim(".$this->session->userdata('id_usuario').") AS acc_ilim");
    $acc_ilim = $acc_ilim->row();
    $params['acc_ilim'] = json_decode($acc_ilim->acc_ilim);
    //precios
    $precios = $this->db->query("SELECT id_precio, precio, tipo, descuento FROM precios ORDER BY id_precio ASC");
    foreach ($precios->result() as $key => $value) {
      if(strpos($value->tipo, 'ilim') !== false){
        $params['precios']['ilim'][$value->tipo] = $value;
      }else{
        $params['precios']['artics'][$value->tipo] = $value;
      }
    }
    $params['barra_right'] = $this->load->view('tic/general/barra_right', $params, true);
    /**** Carro de compras ****/


    $this->load->model('paquetes_model');
    $params['visitas'] = $this->paquetes_model->get_visitas_articulos($this->session->userdata('id_usuario'), '5');

    $params['infogramas'] = $this->skin_model->getInfografias('3');

    // Obtiene el total de mensajes pendientes por responder
    $params['total_mgs'] = $this->skin_model->get_total_mensajes_pendientes();

    $this->load->library('my_facebook');
    $params['fb_app_id'] = $this->my_facebook->APP_ID;

    // var_dump($data);
    $params['info'] = $data;

    if ($data['tipo'] === 'a')
      $params['html'] = $this->get_publicacion($data['info']['info'][0]->id_articulo);

    $this->load->view('tic/header', $params);
    $this->load->view('tic/general/menu', $params);
    $this->load->view('skin/publico/tic', $params);
    $this->load->view('tic/footer');
  }


  public function get_publicacion($id)
  {
    $this->load->model('skin_model');

    $params['arti'] = $this->db->query("SELECT
        a.id_articulo, a.id_tipo, a.numero, a.materia, a.variables,
        a.titulo, a.descripcion, a.fecha_creacion, a.imagen1, a.imagen2, a.imagen3, at.nombre,
        ticDiasFaltantes(".$this->session->userdata('id_usuario').", a.id_articulo ) AS acceso
      FROM articulos AS a INNER JOIN articulos_tipo AS at ON a.id_tipo = at.id_tipo
      WHERE a.id_articulo = ".$id)->row();
    $params['arti']->acceso = json_decode($params['arti']->acceso);
    $fecha = explode("-", $params['arti']->fecha_creacion);
    $params['arti']->fecha_creacion = String::mes($fecha[1])." ".$fecha[0];

    //registra la visita a algun articulo
    // $this->skin_model->registraVisita($this->session->userdata('id_usuario'), $id, $this->session->userdata('idunico') );

    return  $this->load->view('tic/publicaciones/leermas', $params, true);
  }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */